import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-11-26
 * Time: 22:32
 */
public class Solution {
    public String convert(String s, int numRows) {
        //处理边界
        if (numRows < 2) {
            return s;
        }
        //当超过1行时
        List<StringBuilder> list = new ArrayList<>();  //每行用StringBuilder装起来，方便后序拼接。
        //根据行数创建StringBuilder的个数
        for (int i = 0; i < numRows; i++) {
            list.add(new StringBuilder());
        }
        //根据Z字形遍历字符串，将字符放入指定的StringBuilder中
        int i = 0;
        int flag = -1;
        for (int j = 0; j < s.length(); j++) {
            char c = s.charAt(j);
            list.get(i).append(c);
            //当遍历到转折点时，进行转向！
            if (i == 0 || i == numRows-1) {
                flag = -flag;
            }
            i += flag;
        }
        //将list中的StringBuilder进行拼接
        StringBuilder ans = new StringBuilder();
        for (int j = 0; j < list.size(); j++) {
            ans.append(list.get(j));
        }
        return ans.toString();
    }
}
